package com.zhhs.galaxyott.activity.movieDetailActivity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.DeviceCenterActivity;
import com.zhhs.galaxyott.adapters.MovieDetailAdapter;
import com.zhhs.galaxyott.beans.MovieDetailRecBean;
import com.zhhs.galaxyott.utils.Logs;

public class MovieDetailHolder implements OnClickListener {

    private Activity mContext;
    MovieDetailPresenter mPresenter;

    GridView lvMovie;

    List<MovieDetailRecBean> listData = new ArrayList<MovieDetailRecBean>();

    ImageView poster;
    TextView poster_name, ename, watch_number, type, director_actor, introdution, operate_collect, operate_purchase,
            operate_vip;

    public MovieDetailHolder(Activity context, MovieDetailPresenter mPresenter) {
        this.mContext = context;
        this.mPresenter = mPresenter;

        initView();
    }

    private void initView() {

        poster = (ImageView) mContext.findViewById(R.id.movie_img);
        poster_name = (TextView) mContext.findViewById(R.id.poster_name);
        ename = (TextView) mContext.findViewById(R.id.ename);
        watch_number = (TextView) mContext.findViewById(R.id.watch_number);
        type = (TextView) mContext.findViewById(R.id.movie_type);
        director_actor = (TextView) mContext.findViewById(R.id.movie_director_actor);
        introdution = (TextView) mContext.findViewById(R.id.movie_introdution);

        operate_collect = (TextView) mContext.findViewById(R.id.operate_collect);
        operate_purchase = (TextView) mContext.findViewById(R.id.operate_purchase);
        operate_vip = (TextView) mContext.findViewById(R.id.operate_vip);

        if (!((MovieDetailActivity) mContext).sp.getIsComboOnline()) {
            operate_vip.setVisibility(View.GONE);
        } else {
            operate_vip.setVisibility(View.VISIBLE);
        }

        lvMovie = (GridView) mContext.findViewById(R.id.detail_grid_movie);
        lvMovie.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                operate_purchase.requestFocus();
            }
        });

        operate_collect.setOnClickListener(this);
        operate_purchase.setOnClickListener(this);
        operate_vip.setOnClickListener(this);
    }

    /** 设置GirdView参数，绑定数据 */
    public void setGridView(MovieDetailAdapter adapter) {
        int length = 125;
        int size = adapter.getCount();
        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        Logs.logD("DEBUG_d", "THIS IS density-->" + density);

        int gridviewWidth = (int) (size * (length + 0) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth,
                LinearLayout.LayoutParams.MATCH_PARENT);
        lvMovie.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        lvMovie.setColumnWidth(itemWidth); // 设置列表项宽

        // 实现单行显示需要在代码中处理，如果使用xml中的“anto_fit”并没有用
        lvMovie.setNumColumns(size); // 设置列数量=列表集合数
        lvMovie.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.operate_collect:
            if (operate_collect.getText().toString().equals("收藏")) {
                mPresenter.operateCollect();
            } else {
                mPresenter.CancelCollect();
            }

            break;

        case R.id.operate_purchase:

            // 鉴权与订购
            mPresenter.getUrl();

            // mPresenter.addWatchRecord();

            break;
        case R.id.operate_vip:

            Intent intent = new Intent();
            intent.setClass(mContext, DeviceCenterActivity.class);
            intent.putExtra("number", 0);
            mContext.startActivity(intent);

            break;

        default:
            break;
        }

    }

}
